package dataAccessLayer.factoryMySQL.daos;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dataAccessLayer.abstractFactory.daos.DAOmeasureUnity;
import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.visualObject.VOmeasureUnity;

public class DAOmeasureUnityMySQL implements DAOmeasureUnity 
{
	private DBManager dbManager;

	public DAOmeasureUnityMySQL(DBManager dbManager) 
	{
		this.dbManager = dbManager;
	}
	
	public int insertMeasureUnity(VOmeasureUnity vOmeasureUnity) 
	{
		int code = -1;

		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = dbManager.getConnection();

		try
		{
			preparedStatement = connection.prepareStatement("INSERT INTO measureUnities (name) VALUES (?)");

			preparedStatement.setString(1, vOmeasureUnity.getName());

			preparedStatement.execute();

			preparedStatement = connection.prepareStatement("SELECT code FROM measureUnities ORDER BY code DESC LIMIT 1");

			resultSet = preparedStatement.executeQuery();

			if(resultSet.next())
			{
				code = resultSet.getInt(1);
			}
			
		}
		catch (SQLException e)
		{
			code=-1;
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		return code;
	}
	
	public ArrayList<VOmeasureUnity> listMeasureUnities() 
	{
		PreparedStatement preparedStatement = null;
		ResultSet resulSet=null;
		Connection connection = dbManager.getConnection();
		ArrayList<VOmeasureUnity> searchMeasureUnity=new ArrayList<VOmeasureUnity>();
		
		
		try 
		{
			preparedStatement = connection.prepareStatement("SELECT code,name FROM measureUnities");			
			resulSet=preparedStatement.executeQuery();
			while(resulSet.next())
			{
				VOmeasureUnity search=new VOmeasureUnity();
				search.setCode(resulSet.getInt(1));
				search.setName(resulSet.getString(2));
				searchMeasureUnity.add(search);
			}
			
			return searchMeasureUnity;
			
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}	
		finally
		{
			try
			{
				
				preparedStatement.close();
				resulSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		return searchMeasureUnity;
	}
	
	public boolean removeMeasureUnity(VOmeasureUnity vOmeasureUnity)
	{
		return false;
	}
	
	public VOmeasureUnity searchMeasureUnityByCode(VOmeasureUnity vOmeasureUnity) 
	{
		PreparedStatement preparedStatement = null;
		ResultSet resulSet = null;
		Connection connection = dbManager.getConnection();

		VOmeasureUnity measureUnitySearch = null;

		try
		{
			preparedStatement = connection.prepareStatement("SELECT code,name FROM measureUnities WHERE code=?");

			preparedStatement.setInt(1, vOmeasureUnity.getCode());

			resulSet = preparedStatement.executeQuery();
			
			if(resulSet.next())
			{
				measureUnitySearch = new VOmeasureUnity();
				measureUnitySearch.setCode( resulSet.getInt(1) );
				measureUnitySearch.setName( resulSet.getString(2) );
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resulSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		return measureUnitySearch;
	}
	
	public VOmeasureUnity searchMeasureUnityByName(VOmeasureUnity vOmeasureUnity) 
	{
		return null;
	}

}
